//自执行函数，变量私有，不污染全局
(function(window){
    //获取元素
    const cateBox = document.querySelector('#menu > ul')
    const box = document.querySelector('#recommen > div >.box')
    // console.log(shopr);
    
    
    //获取菜单列表
    getMenu()
    function getMenu(){
    ajax({
        url:"http://chst.vip:1234/api/getindexmenu",
        success(res){
            res = res.result
            binList(res)
        }
    })
    }
    
    //使用 res 数据渲染结构
    function binList(res){
        let str = ''
    
       res.forEach((item,index) =>{
           str += `
           <li>
                    <a href="${item.titlehref}">
                        ${item.img}
                        <p>${item.name}</p>
                    </a>
           </li>
           `
       }) 
       cateBox.innerHTML = str
    }
    
    
    getCommodity()
    function getCommodity(){
        ajax({
            url:'http://chst.vip:1234/api/getmoneyctrl',
            success(res){
                resArr = res.result
                discountList(resArr)
            }
        })
    }
    
    function discountList(resArr){
        let sar = ''
    
        resArr.forEach((item,index) =>{
            sar += ` 
            <div class="shop"productid=${item.productId} >
            <div class="s-left">${item.productImgSm}</div>
            <div class="s-right">
            <ul>
            <li><a href="#">${item.productName}<p>${item.productPinkage}</p></a></li>
            </ul>
            <div class="s-img">
                <span>${item.productFrom}</span>
                <a href="${item.productComCount}"><img src="../pages/images/微信图片_20210620172131.png" alt=""></a>
            </div>
         </div>
        </div>
            `
        })
        box.innerHTML = sar;
        [...box.children].forEach(li =>{
            li.onclick = function(){
                 //获取每个元素上面的productid
                 console.log(this);
                 let productid = this.getAttribute('productid')
                 //跳转页面
                 console.log(productid);
                 location.href = `moneyctrl.html?productid=${productid}`
                                  
            }
        })
    }
    
    })(window)